package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author bairimengchang
 * @version 1.0
 * @project sky-take-out
 * @description C端购物车控制器
 * @date 2024/08/22 20:22:09
 */
@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端购物车接口")
public class ShoppingCartController {


    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCartDTO C端添加购物车数据传输数据模型
     * @return 添加购物车成功或失败信息
     */
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result<String> addShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车");
        boolean b = shoppingCartService.addShoppingCart(shoppingCartDTO);
        if (b) {
            return Result.success("添加购物车成功！");
        }
        return Result.error("添加购物车失败，后端控制器报错！");
    }

    /**
     * 查看购物车
     * @return 购物车列表
     */
    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> getShoppingCartList(){
        //不传参数是因为后台能获取到当前登录用户的id，不需要请求头带token
        log.info("查看购物车");
        List<ShoppingCart> shoppingCartList = shoppingCartService.getShoppingCartList();
        return Result.success(shoppingCartList);
    }

    /**
     * 清空购物车
     * @return 清空购物车成功或失败信息
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result<String> cleanShoppingCart(){
        log.info("清空购物车");
        boolean b = shoppingCartService.cleanShoppingCart();
        if (b) {
            return Result.success("清空购物车成功！");
        }
        return Result.error("清空购物车失败，后端控制器报错！");
    }

    /**
     * 删除购物车
     * @param shoppingCartDTO C端删除购物车数据传输数据模型
     * @return 删除购物车成功或失败信息
     */
    @ApiOperation("删除购物车")
    @PostMapping("/sub")
    public Result<String> subShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车");
        boolean b = shoppingCartService.subShoppingCart(shoppingCartDTO);
        if (b) {
            return Result.success("删除购物车成功！");
        }
        return Result.error("删除购物车失败，后端控制器报错！");
    }

}
